<?php

include('common.php');

html_header("page overview");

$english_name_row = sql_row1("SELECT English_Name FROM Definitions WHERE Fxn_Call='$domain' AND User='$user' ");
$english_name = $english_name_row['English_Name'];
$insert_message = 'add here';

?>

<body>

<div id="container">

<?php header_div(); ?>

<div id="body">

<?php
echo "<h4>$english_name</h4>\n";


function display_instance($row)
{
	global $vars, $insert_message, $G;
	$domain = $row['Domain'];
	$line = $row['Line_Number'];
	$user = $row['User'];
	$fxn_call = $row['Fxn_Call'];
	
	$def = sql_row1("SELECT * FROM Definitions WHERE Fxn_Call='$fxn_call' AND (User='$user' OR User IS NULL)");
	$lines = array();
	
	// call's name
	switch ($fxn_call) {
	case 'if':
		$english_name = 'If';
		break;
	case 'while':
		$english_name = 'While';
		break;
	case 'database':
		// needed because name needs to fit in diamond
		$english_name = 'Table';
		break;
	case 'form':
		// same as above
		$english_name = 'Form';
		break;
	case 'for':
		// same as above
		$english_name = 'Count Through';
		break;
	case 'ul':
		$english_name = 'Bulleted List';
		break;
	case 'ol':
		$english_name = 'Numbered List';
		break;
	case 'select':
		$english_name = 'Menu';
		break;
	case 'p':
		$english_name = 'Paragraph';
		break;
	default:
		$english_name = $def['English_Name'];
	}
	
	// add name to lines array
	$lines['Name'] = $english_name;
		

	if (!ctl_struct($fxn_call))
	{
		switch ($fxn_call) {
		case 'arithmetic':
			$var1_value = display_variable($domain, $line, 'Var1');
			$var2_id = $row['Var2'];
			$var3_value = display_variable($domain, $line, 'Var3');
			$text = arithmetical_expression($var1_value, $var2_id, $var3_value);
			$lines[] = $text;

			// add the return value
			$text_y += $text_size + 7;
			$return = display_variable($domain, $line, 'Returns_To');
			$text = "Store in: $return";
			$lines[] = $text;
			break;
		case 'form_input':
			$text = "Value: " . $row['Var1_Text'];
			$lines[] = $text;
			$text = "Caption: " . $row['Var2_Text'];
			$lines[] = $text;
			break;
		case 'log':
			// add this special case so that we can print Base: 10 in case no base is specified (10 is the default)
			// first line: 'Number: 35'
			$line_one = 'Number: ' . display_variable($domain, $line, 'Var1');
			$line_two = display_variable($domain, $line, 'Var2');
			if (empty($line_two))
			{
				$line_two = 'Base: 10 (Default)';
			} else {
				$line_two = 'Base: ' . $line_two;
			}
			$line_three = display_variable($domain, $line, 'Returns_To');
			if (!empty($line_three))
			{
				$line_three = 'Returns To: ' . $line_three;
			}
			//$text = "<ul>\n<li>$line_one</li>\n<li>$line_two</li><li>$line_three</li>\n";
			$lines[] = $line_one;
			$lines[] = $line_two;
			$lines[] = $line_three;
			break;
		case 'round':
			// similar to log, special case because of the default (zero digits after decimal, argument 2)
			$line_one = 'Number: ' . display_variable($domain, $line, 'Var1');
			$line_two = display_variable($domain, $line, 'Var2');
			if (empty($line_two))
			{
				$line_two = 'Digits: 0 (Default)';
			} else {
				$line_two = 'Digits: ' . $line_two;
			}
			$line_three = display_variable($domain, $line, 'Returns_To');
			if (!empty($line_three))
			{
				$line_three = 'Returns To: ' . $line_three;
			}
			//$text = "<ul>\n<li>$line_one</li>\n<li>$line_two</li><li>$line_three</li>\n";
			$lines[] = $line_one;
			$lines[] = $line_two;
			$lines[] = $line_three;
			break;
		case 'delete_from_table':
			$bonus_field = $row['Bonus_Field'];
			$table = remove_userno(delete_from_table_table($bonus_field));
			$id = delete_from_table_default_id($bonus_field);
			$variable_row = sql_row1("SELECT Name FROM Variables WHERE ID='$id' ");
			$variable = $variable_row['Name'];
			if (!empty($variable))
			{
				$line_one = "Delete $variable"; 
			}

			if (!empty($table))
			{
				$line_two = "From $table"; 
			}
						
			$column = delete_from_table_default_column($bonus_field);
			if (!empty($column))
			{
				$line_three = "In column " . $column;
			}
			//$text = "<ul>\n<li>$line_one</li>\n<li>$line_two</li><li>$line_three</li>\n";
			$lines[] = $line_one;
			$lines[] = $line_two;
			$lines[] = $line_three;
			break;
		case 'store_constant':
			$bonus_field_row = sql_row1("SELECT Bonus_Field FROM Instances WHERE Domain='$domain' AND User='$user' AND Line_Number='$line' ");
			$bonus_field = $bonus_field_row['Bonus_Field'];
			$lines[] = "Variable: " . display_variable($domain, $line, 'Returns_To');
			$lines[] = "Constant: " . $bonus_field;
			break;
		default:
			// make text line by line
			$everything = $vars;
			$everything[] = 'Returns_To';
			foreach ($everything as $v)
			{
				if (!is_null($def[$v]))
				{
					// handle the exception:
					// fxn call is form, var is var1 (use Destination_Page in Instances)
					if ($fxn_call == 'form' && $v == 'Var1')
					{
						$desc = 'To';
						$dp = $row['Var1'];
						$destination_page_def = sql_row1("SELECT * FROM Definitions WHERE ID='$dp' ");
						$var_name = $destination_page_def['English_Name'];
					} else { 
						$desc = $def[$v];
						$var_name = display_variable($domain, $line, $v);
					}
					if ($var_name !== FALSE)
					{
						$var_full = $desc . ": " . $var_name;
					} else {
						$var_full = '';
					}
					
					if (!empty($var_full))
					{
						$lines[] = $var_full;
					}
					
				} // end if (!is_null($def[$v]))
			} // end for
			// if there was nothing, this might be an end control structure
		} // end switch
	} else {
		// the case where this call is a control structure 
		// make text all at once for 'if' or 'while'
		if ($fxn_call == 'if' || $fxn_call == 'while')
		{
			$var1_value = display_variable($domain, $line, 'Var1');
			$var2_id = $row['Var2'];
			$var3_value = display_variable($domain, $line, 'Var3');
			$text = equality($var1_value, $var2_id, $var3_value);
		} elseif ($fxn_call == 'database') {
			// table name
			$parsed = parse_query($row['Bonus_Field']);
			$table = $parsed['table'];
			$text = remove_userno($table);
		} elseif ($fxn_call == 'for') {
			$var1_value = display_variable($domain, $line, 'Var1');
			$var2_value = display_variable($domain, $line, 'Var2');
			$var3_value = display_variable($domain, $line, 'Var3');
			if (!empty($var1_value) && !empty($var1_value) && !empty($var1_value))
			{
				$text = "Go from $var2_value to $var3_value using $var1_value";
			} else {
				$text = "[Incomplete]";
			}
		} elseif ($fxn_call == 'form') { 
			// get the definition ID from Var1 in Instance and get English_Name from that definition
			$def_id = $row['Var1'];
			$form_def = sql_row1("SELECT English_Name FROM Definitions WHERE ID='$def_id'");
			$text = $form_def['English_Name'];
		} elseif ($fxn_call == 'select') {
			$text = $row['Var2_Text'];
		} elseif ($fxn_call == 'p') {
			$text = 'Paragraph';
		}
		$lines[] = $text;
	}

	if (!end_ctl_struct($fxn_call))
	{
		$uri = instance_uri($fxn_call, $line);
		$lines['Edit'] = "<a href=\"$uri\">edit</a>";
	}
	$lines['Add'] = "<a href=\"add_instance.php?domain=$domain&amp;line=$line\">$insert_message</a>\n";
	
	$add = $lines['Add'];
	$edit = $lines['Edit'];
	$lines['Add'] = '';
	$lines['Edit'] = '';
	if (empty($G['print']) && !empty($add))
	{
		echo "<p>$add</p>\n";
	}
	
	// now actually display the lines properly
	//var_dump($lines);
	// this if limits the statements to non-end-ctl-structure statements
	if (!empty($lines['Name']))
	{		
		if (empty($G['print']))
		{
			echo "<li>" . $lines['Name'] . " [ $edit ]</li>\n";
		} else {
			echo "<li>" . $lines['Name'] . "</li>\n";
		}
		$lines['Name'] = '';
	}
	if (count($lines) > 1)
	{
		echo "<ul>\n";
		foreach ($lines as $the_line)
		{
			if (!empty($the_line))
			{
				echo "<li>$the_line</li>\n";
			}
		}
		echo "</ul>";
	}
}

$result = sql_exec("SELECT * FROM Instances WHERE Domain='$domain' AND User='$user' ORDER BY Line_Number");

if (mysql_num_rows($result) == 0)
{
	echo "<p><a href=\"add_instance.php?domain=$domain&amp;line=1\">add something</a> (you need to add something)</p>\n";
} else {
	echo "<ol>\n";
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$call = $row['Fxn_Call'];
		$line = $row['Line_Number'];

		display_instance($row);
		if (end_ctl_struct($call)) 
		{
			echo "</ol>\n";
		} else {
			if (ctl_struct($call)) 
			{
				echo "<ol>\n";
			} 
		}
	}
	$line++;
	if (empty($G['print']))
	{
		echo "<p><a href=\"add_instance.php?domain=$domain&amp;line=$line\">$insert_message</a></p>\n";
	}
	echo "</ol>\n";
}

echo "<h4>stuff</h4>\n";
$execute_uri = $domain . '.php';
$execute_href = '<a href="' . $execute_uri . '">admire</a> your handiwork';
echo "<p>$execute_href</p>\n";
echo "<p><a href=\"edit_page.php?domain=$domain&print=yes\">compact overview</a></p>\n";
echo "<p><a href=\"index.php\">home</a></p>\n";
echo "<p><a href=\"memory.php?domain=$domain\">variables</a></p>\n";
echo footer(); ?>

</div>
</div>
</body>
</html>
